using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bamboo.Utils;

namespace Bamboo.ExeShell
{
    public class Plink: PuttyBase
    {
        /// <summary>
        /// 在命令行开头追加. .bash_profile
        /// 默认开启
        /// </summary>
        public bool UseBashProfile { get; set; } = true;

        /// <summary>
        /// 在命令行开头追加export LANG=zh_CN.gbk
        /// 空表示不追加
        /// 默认为zh_CN.gbk
        /// </summary>
        public string ExportLANG { get; set; } = "zh_CN.gbk";

        public string PlinkFilename { get; set; } = "plink.exe";

        /// <summary>
        /// 执行SSH脚本, 可以是多行的
        /// </summary>
        /// <param name="command"></param>
        /// <param name="echoOff"></param>
        /// <param name="sync"></param>
        public void ExecuteCommand(string command, bool echoOff = false, bool sync = false)
        {
            if (command.Contains("\r\n"))
                command = command.Replace("\r\n", "\n");
            //使用临时文件执行
            string tmpFileName = ProcessExecute.GetTempFile("_puttybase", ".sh");
            try
            {
                string header = "";
                if (UseBashProfile)
                {
                    header = ". /etc/profile\n. .bash_profile\n";
                }
                if (!string.IsNullOrWhiteSpace(ExportLANG))
                {
                    header += $"export LANG={ExportLANG}\n";
                }
                command = header + command;
                Log($"SSH信息  IP：{Hostname} 端口：{Port} 用户：{Username}； 以下是待执行的Shell脚本：\r\n" + command.Replace("\n", "\r\n"));
                System.IO.File.WriteAllText(tmpFileName, command, Encoding.GetEncoding("gbk"));
                ExecuteFile(tmpFileName, echoOff, sync);
            }
            finally
            {
                try
                {
                    if (System.IO.File.Exists(tmpFileName)) System.IO.File.Delete(tmpFileName);
                }
                catch (Exception)
                {

                }
            }
        }
        
        public void ExecuteFile(string filename, bool echoOff = false, bool sync = false)
        {
            if (!System.IO.File.Exists(filename))
                throw new Exception("待执行的shell脚本文件不存在：" + filename);
            Cmd.EchoOff = echoOff;
            if (SSHAuthenticationType == PuttyBase.SSHAuthenticationEnum.密码)
            {
                string passwd = $"\"{Password.Replace("\"","\"\"")}\"";
                Cmd.MaskWords.Add($"-pw {passwd}");
                CheckAndRun(Cmd, PlinkFilename, $"-batch -ssh -P {Port} {Username}@{Hostname} -m \"{filename}\" -pw {passwd}");
            }
            else
            {
                CheckAndRun(Cmd, PlinkFilename, $"-batch -ssh -P {Port} -i {PrivateKeyPath} {Username}@{Hostname} -m \"{filename}\"");
            }
        }

    }
}
